package com.iqoo.store.utils;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import java.sql.Connection;
import java.sql.SQLException;

public class DataSourceUtils {
    private static ComboPooledDataSource dataSource = new ComboPooledDataSource();

    static {
        try {
            // 加载数据库驱动
            dataSource.setDriverClass("com.mysql.cj.jdbc.Driver");
            // 设置数据库连接URL
            dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/iqoo_store_wangshuo?useSSL=false&serverTimezone=UTC");
            // 设置数据库用户名
            dataSource.setUser("root");
            // 设置数据库密码
            dataSource.setPassword("123456");
            // 设置连接池参数
            dataSource.setInitialPoolSize(5);
            dataSource.setMaxPoolSize(20);
            dataSource.setMinPoolSize(5);
            dataSource.setMaxIdleTime(300);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static ComboPooledDataSource getDataSource() {
        return dataSource;
    }

    public static Connection getConnection() throws SQLException {
        return dataSource.getConnection();
    }
}